//
//  CZStatusRetweetView.swift
//  CZ29VVeibo
//
//  Created by apple on 16/6/29.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit
class CZStatusRetweetView: UIView {
    // 声明一个全局的转发微博底部约束
    var retweetViewBottomConstraint: Constraint?
    // 供外界给其赋值
    var status: CZStatus?{
        didSet{
            contentLabel.attributedText = status?.retweeted_status?.textAttr
            /*
                - 卸载当前转发微博底部约束
                - 如果status?.retweeted_status?.pic_urls.count > 0 代表有配图
                    - 显示配图
                    - 赋值
                    - 更新约束
                - 反之没有配图
                    - 隐藏配图
                    - 更新约束
            */
            
            // 卸载约束
            retweetViewBottomConstraint?.uninstall()
            // 有配图
            if status?.retweeted_status?.pic_urls?.count > 0 {
                // 显示配图
                pictureView.hidden = false
                // 赋值
                pictureView.pic_urls = status?.retweeted_status?.pic_urls
                // 更新约束
                self.snp_makeConstraints { (make) -> Void in
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(StautsMargin).constraint
                }
            }else {
                // 没有配图
                // 隐藏配图
                pictureView.hidden = true
                // 更新约束
                self.snp_makeConstraints { (make) -> Void in
                    retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(StautsMargin).constraint
                }
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RBG(237, green: 237, blue: 237)
        // 01- 添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        // 02- 添加约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(StautsMargin)
            make.top.equalTo(self).offset(StautsMargin)
        }
        // 配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(contentLabel.snp_bottom).offset(StautsMargin)
            make.leading.equalTo(contentLabel)
        }
        // 给自己添加约束
        self.snp_makeConstraints { (make) -> Void in
            retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(StautsMargin).constraint
        }
        // 测试数据
//        contentLabel.text = "懒加载控件懒加载控件懒加载控件懒加载控件懒加载控件懒加载控件懒加载控件懒加载控件懒加载控件控件懒加载控件控件懒加载控件控件懒加载控件控件懒加载控件控件懒加载控件控件懒加载控件"
    }
    
    // MARK: - 懒加载控件
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15, maxWidth: SCREENW - 2*StautsMargin)
    // 配图
    private lazy var pictureView: CZPictureView = CZPictureView()
    
}
